import { IconGroupEntry } from "./IconGroupEntry.ts";

export class IconGroup {
  idReserved?: number // 必须为0
  idType?: number // 必须为1，表示ICO文件
  idCount?: number // 图标数量
  entries: IconGroupEntry[] = []
  dataReader: IPEFileReader;
  offset: number;

  constructor(dataReader: IPEFileReader) {
    this.dataReader = dataReader
    this.offset = dataReader.getOffset()
  }

  public async read() {
    this.dataReader.setOffset(this.offset)
    const byteArray: IByteArray = await this.dataReader.read(6);
    this.idReserved = byteArray.readWord()
    this.idType = byteArray.readWord()
    this.idCount = byteArray.readWord()
    for (let i = 0; i < this.idCount; i++) {
      this.entries.push(new IconGroupEntry(await this.dataReader.read(14)))
    }
  }

  public getData(): Uint8Array {
    const dataView = new DataView(new ArrayBuffer(6))
    dataView.setUint16(0, this.idReserved!, true)
    dataView.setUint16(2, this.idType!, true)
    dataView.setUint16(4, 1, true)
    return new Uint8Array(dataView.buffer)
  }
}
